﻿using System;
using System.Collections.Generic;
using System.Text;
using Evl.Data.EventArgs;

namespace Evl.Data.ModelBase
{
    public partial  class ElvModelBase
    {

        public ElvModelBase() { }

        #region 必要属性
        public String TableName { get; set; }
        protected String TableType { get; set; }

        /// <summary>
        /// 子类必须重写该方法
        /// </summary>
        public virtual void SetPropertyName()
        {
 
        }

        private bool[] ChangeList;

        private String[] _filedsList;
        /// <summary>
        /// 初始化字段列表
        /// 
        /// </summary>
        public String[] FiledsList
        {
            get
            {
                if (_filedsList == null)
                {
                    SetPropertyName();
                    ChangeList = new bool[FiledsList.Length];
                }
                return _filedsList;
            }
            set { _filedsList = value; }
        }

        protected T GetProperty<T>(string PropertyName)
        {
            //添加属性变化事件
            OnpropertyGetting(PropertyName);
            //在装箱列表中获取值
            object obj = GetPropertyValue(PropertyName);
            //值拆箱并且返回
            return ConvertValue<T>(obj);
        }

        private T ConvertValue<T>(object obj)
        {
            if (obj != null)
                return (T)obj;
            else if (obj == DBNull.Value || obj == null)
            {
                if (typeof(T) == typeof(DateTime))
                {
                    object o = DateTime.MinValue;
                    return (T)o;
                }
                else
                    return default(T);
            }
            else
            {
                //如果 Value为 decimal类型，Ｔ　为double 类型， (T)Value 将发生错误
                return (T)Convert.ChangeType(obj, typeof(T));
            }
        }

        private object GetPropertyValue(string PropertyName)
        {
            for (int i = 0; i < FiledsList.Length; i++)
            {
                if (FiledsList[i] == PropertyName)
                {
                    return FiledsValueArray[i];
                }
            }
            return new object();
        }
        //定义值装箱数组
        private object[] _filedsValueArray;
        public object[] FiledsValueArray
        {
            get
            {
                if (_filedsValueArray == null)
                {
                    _filedsValueArray = new object[FiledsList.Length];
                }
                return _filedsValueArray;
            }
            set
            {
                _filedsValueArray = value;
            }
        }

        //属性名称被使用时促发事件
        public event EventHandler<PropertyGettingEventArgs> PropertyGetting;
        public void OnpropertyGetting(string Name)
        {
            if (this.PropertyGetting != null)
            {
                this.PropertyGetting(this, new PropertyGettingEventArgs(Name));
            }
        }

        protected void SetProperty(string PropertyName, object obj)
        {
            //1.装箱,并且保存值到装箱数组
            //2.设置属性名称到属性改变列表中
            SaveValue(PropertyName, obj);
        }

        private void SaveValue(string PropertyName, object obj)
        {
            for (int i = 0; i < FiledsList.Length; i++)
            {
                if (String.Compare(PropertyName, FiledsList[i]) == 0)
                {
                    ChangeList[i] = true;
                    FiledsValueArray[i] = obj;
                    break;
                }
            }
        }


        #endregion
        public bool HasFildName(String FiledName)
        {
            foreach (String item in FiledsList)
            {
                if (item==FiledName)
                {
                    return true;
                }
            }
            return false;
        }


    }
}
